home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / ham / sattrk31.tgz / sattrack-3.1.tar / SatTrack / src / makeamsat / makeamsat.c
C/C++ Source or Header  |  1995-03-16  |  11KB  |  285 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*  Title       : makeamsat.c                                                 */
  4. /*  Author      : Manfred Bester                                              */
  5. /*  Date        : 24Feb92                                                     */
  6. /*  Last change : 15Mar95                                                     */
  7. /*                                                                            */
  8. /*  Synopsis    : This program reads a file with Keplerian elements in the    */
  9. /*                NORAD 2-line format and generates one in the AMSAT format.  */
  10. /*                                                                            */
  11. /*  Input file  : tle.dat or sts-47.dat (for example)                         */
  12. /*  Output file : amsat.dat                                                   */
  13. /*                                                                            */
  14. /*                                                                            */
  15. /*  SatTrack is Copyright (c) 1992, 1993, 1994, 1995 by Manfred Bester.       */
  16. /*  All Rights Reserved.                                                      */
  17. /*                                                                            */
  18. /*  Permission to use, copy, and distribute SatTrack and its documentation    */
  19. /*  in its entirety for educational, research and non-profit purposes,        */
  20. /*  without fee, and without a written agreement is hereby granted, provided  */
  21. /*  that the above copyright notice and the following three paragraphs appear */
  22. /*  in all copies. SatTrack may be modified for personal purposes, but        */
  23. /*  modified versions may NOT be distributed without prior consent of the     */
  24. /*  author.                                                                   */
  25. /*                                                                            */
  26. /*  Permission to incorporate this software into commercial products may be   */
  27. /*  obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,    */
  28. /*  Berkeley, CA 94709, USA. Note that distributing SatTrack 'bundled' in     */
  29. /*  with ANY product is considered to be a 'commercial purpose'.              */
  30. /*                                                                            */
  31. /*  IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, */
  32. /*  SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF   */
  33. /*  THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED  */
  34. /*  OF THE POSSIBILITY OF SUCH DAMAGE.                                        */
  35. /*                                                                            */
  36. /*  THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT      */
  37. /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A   */
  38. /*  PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"      */
  39. /*  BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, */
  40. /*  UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                  */
  41. /*                                                                            */
  42. /******************************************************************************/
  43.  
  44. #include <stdio.h>
  45. #include <math.h>
  46. #include <string.h>
  47.  
  48. #ifndef STDLIB
  49. #include <stdlib.h>
  50. #endif
  51.  
  52. #include "sattrack.h"
  53.  
  54. #ifdef HPTERM
  55. #include "hpterm.h"
  56. #else
  57. #include "vt100.h"
  58. #endif
  59.  
  60. extern double getElement();
  61. extern void   mGets();
  62.  
  63. void main()
  64.  
  65. {
  66.     double epochDay, decayRate, inclination, raan, eccentricity;
  67.     double argPerigee, meanAnomaly, meanMotion, epochYear;
  68.  
  69.     int    lineNum, lineNum1, lineNum2, satNum, elementSet, orbitNum;
  70.     int    i, nSat, checkValue, checkSum, checkSumTotal, ephemerisType;
  71.  
  72.     char   satName[100], line1[100], line2[100], data[100], tle[100];
  73.     char   inputData[20], inputFile[100], outputFile[100], str[80], strng[10];
  74.     char   sysComm[100];
  75.     char   *strpHome, *getenv();
  76.  
  77.     FILE   *InputFile, *OutputFile;
  78.  
  79. #ifdef HOMEDIR
  80.     strpHome = getenv("HOME");
  81. #else
  82.     strpHome = SATDIR;
  83. #endif
  84.  
  85.     sprintf(data,"%s/%s",strpHome,DATA);
  86.     sprintf(tle,"%s/%s",strpHome,TLE);
  87.  
  88.     nl();
  89.  
  90.     sprintf(sysComm,"cd %s; ls *.dat",tle);
  91.     system(sysComm);
  92.  
  93.     printf("\nenter input data file       (e.g. tle.dat): ");
  94.     mGets(inputData);
  95.     sprintf(inputFile,"%s/%s",tle,inputData);
  96.  
  97.     if ((InputFile = fopen(inputFile,"r")) == NULL)
  98.     {
  99.         nl(); doBeep(); reverseBlink();
  100.         printf("%s not found\n",inputFile);
  101.         normal(); nl();
  102.         exit(-1);
  103.     }
  104.  
  105.     sprintf(outputFile,"%s/amsat.dat",data);
  106.  
  107.     if ((OutputFile = fopen(outputFile,"w")) == NULL)
  108.     {
  109.         nl(); doBeep(); reverseBlink();
  110.         printf("can't write %s\n",outputFile);
  111.         normal(); nl();
  112.         exit(-1);
  113.     }
  114.  
  115.     printf("\ncreating %s ....\n\n",outputFile);
  116.     nSat = 0;
  117.  
  118.     while (fgets(satName,80,InputFile))
  119.     {
  120.         if (!strncmp(satName,TLEHEADER,10))
  121.             fgets(satName,80,InputFile);
  122.  
  123.         fgets(line1,80,InputFile);
  124.         fgets(line2,80,InputFile);
  125.  
  126.         sscanf(line1,"%1d",&lineNum1);
  127.         sscanf(line2,"%1d",&lineNum2);
  128.  
  129.         if (lineNum1 != 1)
  130.             printf("Line 1 not available for satellite %s",satName);
  131.  
  132.         if (lineNum2 != 2)
  133.             printf("Line 2 not available for satellite %s",satName);
  134.  
  135. /******************************************************************************/
  136. /*                                                                            */
  137. /* calculate checksum                                                         */
  138. /*                                                                            */
  139. /******************************************************************************/
  140.  
  141.         if (lineNum1 == 1 && lineNum2 == 2)
  142.         {
  143.             checkSumTotal = 0;
  144.  
  145.             for (lineNum = 1; lineNum <=2; lineNum++)
  146.             {
  147.                 checkSum = 0;
  148.  
  149.                 if (lineNum == 1)
  150.                     sprintf(str,"%s",line1);
  151.                 if (lineNum == 2)
  152.                     sprintf(str,"%s",line2);
  153.  
  154.                 for (i = 0; i < 68; i++)
  155.                 {
  156.                     strng[0]   = str[i];
  157.                     strng[1]   = '\0';
  158.                     checkValue = atoi(strng);
  159.  
  160.                     if (!strcmp(strng,"-"))
  161.                         checkValue = 1;      /* assign check sum value to '-' */
  162.  
  163.                     checkSum += checkValue;
  164.                 }
  165.  
  166.                 strng[0] = str[68];
  167.                 strng[1] = '\0';
  168.  
  169.                 if (checkSum % 10 != atoi(strng))
  170.                 {
  171.                     doBeep(); reverseBlink();
  172.                     printf("checksum error in line %d for satellite %s",
  173.                             lineNum,satName);
  174.                     normal();
  175.                 }
  176.  
  177.                 checkSumTotal += checkSum;
  178.             }
  179.  
  180. /******************************************************************************/
  181. /*                                                                            */
  182. /* get elements from lines 1 and 2                                            */
  183. /*                                                                            */
  184. /******************************************************************************/
  185.  
  186.             satNum        = getElement(line1, 3, 8);
  187.             epochYear     = getElement(line1,19,20);
  188.             epochDay      = getElement(line1,21,32);
  189.             decayRate     = getElement(line1,34,43);
  190.             ephemerisType = getElement(line1,63,63);
  191.             elementSet    = getElement(line1,65,68);
  192.  
  193.             inclination   = getElement(line2, 9,16);
  194.             raan          = getElement(line2,18,25);
  195.             eccentricity  = getElement(line2,27,33);
  196.             argPerigee    = getElement(line2,35,42);
  197.             meanAnomaly   = getElement(line2,44,51);
  198.             meanMotion    = getElement(line2,53,63);
  199.             orbitNum      = getElement(line2,64,68);
  200.  
  201.             epochDay     += epochYear * 1000.0;
  202.             eccentricity *= 1.0e-7;
  203.  
  204.             fprintf(OutputFile,"Satellite: %s",satName);
  205.             fprintf(OutputFile,"Catalog number: %10d\n",satNum);
  206.             fprintf(OutputFile,"Epoch time: %14.8f\n",epochDay);
  207.             fprintf(OutputFile,"Element set: %13d\n",elementSet);
  208.             fprintf(OutputFile,"Inclination: %13.4f deg\n",inclination);
  209.             fprintf(OutputFile,"RA of node: %14.4f deg\n",raan);
  210.             fprintf(OutputFile,"Eccentricity: %12.7f\n",eccentricity);
  211.             fprintf(OutputFile,"Arg of perigee: %10.4f deg\n",argPerigee);
  212.             fprintf(OutputFile,"Mean anomaly: %12.4f deg\n",meanAnomaly);
  213.             fprintf(OutputFile,"Mean motion: %13.8f rev/day\n",meanMotion);
  214.             fprintf(OutputFile,"Decay rate: %14.3e rev/day^2\n",decayRate);
  215.             fprintf(OutputFile,"Epoch rev: %15d\n",orbitNum);
  216.             fprintf(OutputFile,"Checksum: %16d\n",checkSumTotal);
  217.             fprintf(OutputFile,"\n");
  218.  
  219.             nSat++;
  220.             printf("%3d: %s",nSat,satName);
  221.         }
  222.     }
  223.  
  224.     fclose(InputFile);
  225.     fclose(OutputFile);
  226.     printf("\noutput file contains data for %d satellite",nSat);
  227.     if (nSat > 1) printf("s");
  228.     nl();
  229.     nl();
  230. }
  231.  
  232. /******************************************************************************/
  233. /*                                                                            */
  234. /* getElement: returns double of orbital element out of ASCII string          */
  235. /*                                                                            */
  236. /******************************************************************************/
  237.  
  238. double getElement(gstring,gstart,gstop)
  239.  
  240. int  gstart, gstop;
  241. char gstring[80];
  242.  
  243. {
  244.     int  k, glength;
  245.     char gstr[80];
  246.  
  247.     glength = gstop - gstart + 1;
  248.  
  249.     for (k = 0; k <= glength; k++)
  250.         gstr[k] = gstring[gstart+k-1];
  251.  
  252.     gstr[glength] = '\0';
  253.  
  254.     return((double) atof(gstr));
  255. }
  256.  
  257. /******************************************************************************/
  258. /*                                                                            */
  259. /* mGets: Manfred's version of fgets (wipes out newline character)            */
  260. /*                                                                            */
  261. /******************************************************************************/
  262.  
  263. void mGets(string)
  264.  
  265. char *string;
  266.  
  267. {
  268.     int i;
  269.     fgets(string,80,stdin);
  270.     i = (int) strlen(string);
  271.  
  272.     if (i > 0)
  273.         string[i-1] = '\0';
  274.     else
  275.         string[0]   = '\0';
  276.  
  277.     return;
  278. }
  279.  
  280. /******************************************************************************/
  281. /*                                                                            */
  282. /* End of program makeamsat.c                                                 */
  283. /*                                                                            */
  284. /******************************************************************************/
  285.